package com.spark.rectify

import android.widget.CheckBox
import android.widget.ImageView
import android.widget.RelativeLayout
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.chad.library.adapter.base.entity.AbstractExpandableItem
import com.chad.library.adapter.base.entity.MultiItemEntity
import com.spark.common.ext.server

class UnsafeActionRespManAdapter(datas: MutableList<MultiItemEntity>) : BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder>(datas) {

    val results = mutableListOf<LevelThree>()

    companion object {
        const val levelOne = 0
        const val levelTwo = 1
        const val levelThree = 2
    }

    init {
        addItemType(levelOne, R.layout.item_unsafe_leve_1)
        addItemType(levelTwo, R.layout.item_unsafe_leve_2)
        addItemType(levelThree, R.layout.item_unsafe_level_3)
    }

    override fun convert(helper: BaseViewHolder?, item: MultiItemEntity?) {
        if (item == null) return
        helper?.apply {
            when (item.itemType) {
                levelOne -> {
                    item as LevelOne
                    setText(R.id.tv_one_name, item.name)
                    val ivOneArr = getView<ImageView>(R.id.iv_one_arr)
                    val levelOne = getView<RelativeLayout>(R.id.level_one)
                    ivOneArr.setOnClickListener {
                        if (item.isExpanded) {
                            collapse(adapterPosition)
                            ivOneArr.setImageResource(R.drawable.ic_arrow_down_blue)
                        } else {
                            expand(adapterPosition)
                            ivOneArr.setImageResource(R.drawable.ic_arrow_up_blue)
                        }
                    }
                    levelOne.setOnClickListener {
                        if (item.isExpanded) {
                            collapse(adapterPosition)
                            ivOneArr.setImageResource(R.drawable.ic_arrow_down_blue)
                        } else {
                            expand(adapterPosition)
                            ivOneArr.setImageResource(R.drawable.ic_arrow_up_blue)
                        }
                    }
                }
                levelTwo -> {
                    item as LevelTwo
                    setText(R.id.tv_two_name, item.name)
                    val ivTwoArr = getView<ImageView>(R.id.iv_two_arr)
                    val levelTwo = getView<RelativeLayout>(R.id.level_two)
                    ivTwoArr.setOnClickListener {
                        if (item.isExpanded) {
                            collapse(adapterPosition)
                            ivTwoArr.setImageResource(R.drawable.ic_arrow_down_blue)
                        } else {
                            expand(adapterPosition)
                            ivTwoArr.setImageResource(R.drawable.ic_arrow_up_blue)
                        }
                    }
                    levelTwo.setOnClickListener {
                        if (item.isExpanded) {
                            collapse(adapterPosition)
                            ivTwoArr.setImageResource(R.drawable.ic_arrow_down_blue)
                        } else {
                            expand(adapterPosition)
                            ivTwoArr.setImageResource(R.drawable.ic_arrow_up_blue)
                        }
                    }
                }
                levelThree -> {
                    item as LevelThree
                    setText(R.id.tv_three_name, "${item.name}  ${item.phone}")
                    if (!item.avatar.isNullOrBlank())
                        getView<ImageView>(R.id.iv_avatar).server(item.avatar)
                    val cb = getView<CheckBox>(R.id.cb)
                    getView<RelativeLayout>(R.id.level_three).setOnClickListener {
                        val beforeChecked = cb.isChecked
                        cb.isChecked = !beforeChecked
                    }
                    cb.setOnCheckedChangeListener { _, isChecked ->
                        if (isChecked) {
                            results.add(item)
                        } else {
                            results.remove(item)
                        }
                    }
                }
            }
        }
    }
}

/**
 * LevelThree
 */
data class LevelThree(
        var id: String = "",
        var avatar: String = "",
        var name: String = "",
        var phone: String = "",
        var isChecked: Boolean = false
) : MultiItemEntity {
    override fun getItemType(): Int {
        return 2
    }
}

/**
 * LevelOne
 */
data class LevelTwo(
        var id: String = "",
        var name: String = ""
) : AbstractExpandableItem<LevelThree>(), MultiItemEntity {
    override fun getLevel(): Int {
        return UnsafeActionRespManAdapter.levelTwo
    }

    override fun getItemType(): Int {
        return 1
    }

}

/**
 * LevelOne
 */
data class LevelOne(
        var id: String = "",
        var name: String = ""
) : AbstractExpandableItem<LevelTwo>(), MultiItemEntity {
    override fun getLevel(): Int {
        return UnsafeActionRespManAdapter.levelOne
    }

    override fun getItemType(): Int {
        return 0
    }

}